//author:kakake
class tsselfpayService {
    public data = {
        dbConn: TSLobsterAPI.Program.GetDbConn(),
        WorkId: 1
    }

    public hisinterface: tshisinterfaceController;
    public dao: tsselfpayDao;

    constructor(workid: number) {
        this.data.WorkId = workid;

        this.hisinterface = new tshisinterfaceController(1);
        this.dao = new tsselfpayDao(this.data.WorkId);
    }

    public PresPayment = {
        "model": {
            "Id": 0,
            "CardNum": "",
            "CardType": 0,
            "PatName": "",
            "Regno": "",
            "Repnos": "",
            "Fee": "",
            "PayStatus": 0,
            "CreateTime": "yyyy-MM-dd",
            "UpdateTime": "yyyy-MM-dd",
            "OrderNo": "",
            "UniqueNumber": "",
            "TranserNo": "",
            "PayMode": 0,
            "OrderState": 0,
            "PayTime": "",
            "WorkId": 0,
            "MpUserId": "",
            "BranchHospitalId": 0
        },
        "tablename": "WPMT_PresPayment",
        "key": "Id",
        "val": "0",
        "objval": {},
        "query": {},
        "where": "",
        "pageinfo": {},
        "WorkId": 1
    }


    public FeeDetail = {
        "model": {
            "FeeDetailId": 0,
            "RecordId": 0,
            "PatListId": "",
            "Repno": "",
            "PresDoctorID": "",
            "PresDoctName": "",
            "PresDeptID": 0,
            "PresDeptName": "",
            "ExecDeptId": "",
            "ExecDeptName": "",
            "FeeTime": "yyyy-MM-dd",
            "ItemName": "",
            "FeeTypeName": 0,
            "UnitPrice": "",
            "Quantity": "",
            "Specs": "",
            "Usage": "",
            "Frequency": "",
            "Unit": "",
            "CreateTime": "yyyy-MM-dd",
            "UpdateTime": "yyyy-MM-dd",
            "WorkId": 0
        },
        "tablename": "WPMT_FeeDetail",
        "key": "FeeDetailId",
        "val": "0",
        "objval": {},
        "query": {},
        "where": "",
        "pageinfo": {},
        "WorkId": 1
    }

    //缴费处方
    public balance(model) {
        
        this.PresPayment.WorkId = this.data.WorkId;
        this.FeeDetail.WorkId = this.data.WorkId;

        // var model = JSON.parse(TSLobsterAPI.Program.RequestBody());
        try {
            this.data.dbConn.BeginTransaction();

            model.Repnos = model.Repnos.join(",");
            model.PayStatus = 1;//1 未支付 2已支付
            model.CreateTime = TSLobsterAPI.Utils.getCurrTime("yyyy-MM-dd HH:mm:ss");
            model.UpdateTime = TSLobsterAPI.Utils.getCurrTime("yyyy-MM-dd HH:mm:ss");
            model.OrderNo = "";//从支付平台获取
            model.UniqueNumber = "";
            model.PayMode = 1;//微信
            model.OrderState = 1;//订单状态: 1待付款、2已付款、5已取消、6退款中、7退款完成、
            model.PayTime = TSLobsterAPI.Utils.getCurrTime("yyyy-MM-dd HH:mm:ss");
            model.MpUserId = TSLobsterAPI.Program.GetMPUserId();
            this.PresPayment.objval = model;
            TSLobsterAPI.DbHelper.SaveModel(this.data.dbConn, this.PresPayment);
            model.Id = this.PresPayment.objval['Id'];
            model.FeeItems.forEach(item => {
                item.RecordId = this.PresPayment.objval['Id'];
                item.CreateTime = TSLobsterAPI.Utils.getCurrTime("yyyy-MM-dd HH:mm:ss");
                item.UpdateTime = TSLobsterAPI.Utils.getCurrTime("yyyy-MM-dd HH:mm:ss");
                this.FeeDetail.objval = item;
                TSLobsterAPI.DbHelper.SaveModel(this.data.dbConn, this.FeeDetail);
            })
            this.data.dbConn.CommitTransaction();

        } catch (e) {
            this.data.dbConn.RollbackTransaction();
            throw e;
        }

        //测试支付成功后调用his接口
        //this.balancepaysuccess(model.Id);
        let workid = TSLobsterAPI.Program.GetWorkId();
        let appid = TSLobsterAPI.Program.GetAppId();
        let ret = TSLobsterAPI.AppFunction.GetSystemConfigs(workid, appid, 'SubMchId');
        // TSLobsterAPI.Utils.TrackLog('SubMchId',ret);
        if(!ret.data.config.SubMchId){
            throw '系统参数SubMchId没有配置';
        }
        //获取收银台页面
        let param = {
            SubMchId: ret.data.config.SubMchId,
            BusinessOrderNo: model.Id.toString(),
            OrderType: 2,//业务类型 1预约挂号 2自助缴费
            PayType: 1,//默认1 微信支付
            PayScene: 1,//1 h5 2小程序 3app
            Price: model.Fee * 100, //单位分
            Body: '处方缴费',//商品描述
            Memo: '备注'//可以不填
        };
        // TSLobsterAPI.Utils.TrackLog('param',param);
        let res = TSLobsterAPI.AppFunction.CallOutService(appid, 'pay.pay.preorder', param);
        TSLobsterAPI.Utils.TrackLog('payredirecturl',res);
        let url = res.data.payredirecturl;//收银台界面，支付成功后回调订阅服务register.register.toregister，然后会调用his接口


        return { result: true, url: url };
    }

    //缴费支付回调
    public balancepaysuccess(backpayobj: Backpayobject) {
        // let Id;
        // let TranSerNo = '';
        // let OrderNo = '';
        let PayMode = 1;
        let PayTime = TSLobsterAPI.Utils.getCurrTime("yyyy-MM-dd HH:mm:ss");

        var info = this.dao.getPresPaymentInfo(parseInt(backpayobj.BusinessOrderNo));

        let param = {
            Repno: info["Repnos"],
            TranserNo: backpayobj.TransactionId,
            PayTime: PayTime,
            PayMode: PayMode,
            payAmt: info["Fee"]
        }

        let ret = this.hisinterface.balancePatPresDetail(param);
        if (ret.result) {
            this.dao.updatePresPayed(parseInt(backpayobj.BusinessOrderNo), backpayobj.TransactionId, backpayobj.OrderNo, PayMode);
        } else {
            throw '调用his接口失败';
        }
        return { result: true };

    }
}